﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <summary>
//   Defines the StringExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.Helpers
{
    using System;

    /// <summary>
    /// String class extension methods.
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Determines whether the specified string is an IP address.
        /// </summary>
        /// <param name="ip">The string.</param>
        /// <returns>
        /// <c>true</c> if the specified IP is IP; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsIP(this string ip)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$\b");
        }

        /// <summary>
        /// Determines is the specified string starts with an IP address.
        /// </summary>
        /// <param name="entry">The string.</param>
        /// <returns>
        /// <c>true</c>if the specified string starts with an IP; otherwise, <c>false</c>.
        /// </returns>
        public static bool StartsWithIP(this string entry)
        {
            string ip;
            return StartsWithIP(entry, out ip);
        }

        /// <summary>
        /// Determines is the specified string starts with an IP address.
        /// </summary>
        /// <param name="entry">The string.</param>
        /// <param name="ip">The parsed ip.</param>
        /// <returns>
        /// <c>true</c>if the specified string starts with an IP; otherwise, <c>false</c>.
        /// </returns>
        public static bool StartsWithIP(this string entry, out string ip)
        {
            try
            {
                if (IsIP(entry))
                {
                    ip = entry;
                    return true;
                }

                var parsedEntry = entry.Split('.');
                string parsedIP = string.Empty;
                for (int i = 0; i <= 3; i++)
                {
                    parsedIP += parsedEntry[i] + ".";
                }

                var index = parsedIP.IndexOf(parsedEntry[3]);

                string lastPart = string.Empty;
                foreach (var chr in parsedEntry[3])
                {
                    int number;
                    if (int.TryParse(chr.ToString(), out number))
                    {
                        lastPart += number.ToString();
                    }
                    else
                    {
                        break;
                    }
                }

                parsedIP = parsedIP.Remove(index) + lastPart;
                ip = parsedIP;
                return IsIP(parsedIP);
            }
            catch (Exception)
            {
                ip = string.Empty;
                return false;
            }
        }
    }
}
